<?php
namespace Admin\Model; 
use Think\Model;
/**
 * 订单模型
 */
class  OrderModel extends Model{
   public function index()
    {
        /*获取该运营商下的所有商家*/
        $yid=!empty($_SESSION['yid'])?$_SESSION['yid']:0;
        $shopid=M('shop_user')->where("founder='%d'",$yid)->getField("shopid",true);
        $shopid=implode(",", $shopid);
        $ordersn=I('post.ordersn','','strip_tags');
        $areatype=I('post.areatype','','strip_tags'); //平台订单，运营商订单
        $paytype=I('post.paytype','','strip_tags');
        $starttime=I('post.starttime','','strip_tags');
        $endtime=I('post.endtime','','strip_tags');
        $orderstatus=I('post.orderstatus','','strip_tags');
        $page=I('post.page',1,'strip_tags');
        $seach_shopid=I('post.shopid',0,'strip_tags');
        //搜索商家id 存在
        $shopid=!empty($seach_shopid)?$seach_shopid:$shopid;
        $condition=' 1=1 ';
        if(!empty($shopid))
        {
            
            $condition.=' AND shopid in '.'('.$shopid.')';
        }

        if(!empty($ordersn))
        {
            $condition.=" AND a.order_sn like '%$ordersn%' ";
        }
        if($areatype !=99 && !empty($areatype))
        {
            $condition.=' AND a.areatype= '.$areatype;
        }
        if($paytype !=99 && !empty($paytype))
        {
            $condition.=' AND a.pay_type='.$paytype;
        }
        if(!empty($starttime) && !empty($endtime))
        {   $starttime=strtotime($starttime);
            $endtime=strtotime($endtime);
            $condition.=' AND a.create_time BETWEEN '.$starttime .' AND '.$endtime ;
        }
        //订单状态
        if($orderstatus !=99 && !empty($orderstatus))
        {   $orderstatus=intval($orderstatus);
            $condition.=" AND (a.shipping_status+a.pay_status+a.order_status)=$orderstatus " ;
        }
        //符合条件的总条数
        $order=M('order a');
        $count=$order->query("select count(*) as count from ".C(DB_PREFIX).'order a where '.$condition);
        $pagearr= getPageArr($page,C('PAGESIZE'),$count[0]['count']);
        $start=$pagearr['start'];
        $pagesize=$pagearr['pagesize'];
        $sql='select a.orderid,a.order_sn,a.shopid,b.number,b.price,b.supperprice,(a.order_status+a.pay_status+a.shipping_status)as allstatus ,a.pay_type,a.areatype,a.shopid,FROM_UNIXTIME(a.create_time, "%Y-%m-%d %H:%i:%S") as create_time from '.C(DB_PREFIX).'order a inner join '.C(DB_PREFIX).'order_item b  
              on a.orderid=b.orderid  where '.$condition." order by a.orderid desc limit  $start , $pagesize";
        
        $orders=M()->query($sql);


       //获取经销商

        if(!empty($orders))
        { 
            $shopuser=M('shop_user');
            foreach ($orders as $key => &$value)
            {
                $value['companyname']=$shopuser->where('shopid=%d',$value['shopid'])->getField('companyname');
                switch ($value['pay_type'])
                {
                    case '0':
                        $value['pay_type']='银行转账';
                        break;
                    case '1':
                        $value['pay_type']='支付宝';
                        break;               
                }  
                switch ($value['areatype'])
                {
                    case '1':
                        $value['area']='现货区';
                        break;
                    case '2':
                        $value['area']='网购区';
                        break;
                    case '3':
                        $value['area']='店长推荐区';
                        break;
                    case '4':
                        $value['area']='乡镇专供区';
                        break;
                    default:
                        $value['area']='网购区';
                        break;
                }             
                switch ($value['allstatus']) {
                    case '0':
                        $value['allstatus']='未确认';
                        break;
                    case '1':
                        //$value['allstatus']='已确认,未付款';
                        $value['allstatus']='未付款';
                        break;
                    case '3':
                        //$value['allstatus']='已确认,已付款';
                        $value['allstatus']='已付款';
                        break;
                    case '6':
                        //$value['allstatus']='已确认,已付款,配货中';
                        $value['allstatus']='配货中';
                        break;
                    case '8':
                        //$value['allstatus']='已确认,已付款,发货中';
                        $value['allstatus']='发货中';
                        break;
                    case '9':
                        $value['allstatus']='已收货';
                        break;
                    case '4':
                        $value['allstatus']='退货';
                        break;
                    case '2':
                        $value['allstatus']='取消';
                        break;
    
                }

            }
        }   
        $result=array();
        $result['pagearr']=$pagearr;
        $result['orders']=$orders;
        return $result;
    }


}